package cn.xiehangs;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.viewport.StretchViewport;

import static java.awt.image.ImageObserver.HEIGHT;
import static java.awt.image.ImageObserver.WIDTH;

public class start implements Screen, jie_mian {
    SpriteBatch batch;
    Stage stage;
    Button.ButtonStyle style1,style2,style3,style4;
    Button btn1,btn2,btn3,btn4;
    Texture imageUp1,imageUp2,imageUp3,imageUp4,background;
    OrthographicCamera cam;
    Texture imageover1,imageover2,imageover3,imageover4;
    String name="start";//用于判断当前是哪个界面
    String next="";//用于判断下一个界面是哪个
    cn.xiehangs.zhu_jie_mian zhu_jie_mian;
//    切屏动画 切屏动画_1;
    public start(){
    }
    public start(cn.xiehangs.zhu_jie_mian zhu_jie_mian){
        this.zhu_jie_mian=zhu_jie_mian;
    }


    @Override
    public void show() {
        batch = new SpriteBatch();
        stage= new Stage();
        stage.setViewport(new StretchViewport(2381/2, 1252/2));
        //改变窗口大小
        Gdx.graphics.setWindowedMode(2381/2, 1252/2);
        cam = new OrthographicCamera(WIDTH, HEIGHT);
        //用于使得摄像机知道屏幕的大小，不然会导致stage调用时backgroud图片渲染出现问题
        cam.setToOrtho(false);
        batch.setProjectionMatrix(cam.combined);
        style1 = new Button.ButtonStyle();
        background = new Texture("start_background.jpg");
        imageUp1 = new Texture("button/本地游戏_up.png");
        imageover1 = new Texture("button/本地游戏_down.png");
        style1.up = new TextureRegionDrawable(new TextureRegion(imageUp1));
        style1.over = new TextureRegionDrawable(new TextureRegion(imageover1));
        style1.down = new TextureRegionDrawable(new TextureRegion(imageUp1));
        style2 = new Button.ButtonStyle();
        imageUp2 = new Texture("button/多人联机_up.png");
        imageover2 = new Texture("button/多人联机_down.png");
        style2.up = new TextureRegionDrawable(new TextureRegion(imageUp2));
        style2.over = new TextureRegionDrawable(new TextureRegion(imageover2));
        style2.down = new TextureRegionDrawable(new TextureRegion(imageUp2));
        style3 = new Button.ButtonStyle();
        imageUp3 = new Texture("button/游戏设置_up.png");
        imageover3 = new Texture("button/游戏设置_down.png");
        style3.up = new TextureRegionDrawable(new TextureRegion(imageUp3));
        style3.over = new TextureRegionDrawable(new TextureRegion(imageover3));
        style3.down = new TextureRegionDrawable(new TextureRegion(imageUp3));
        style4 = new Button.ButtonStyle();
        imageUp4 = new Texture("button/退出游戏_up.png");
        imageover4 = new Texture("button/退出游戏_down.png");
        style4.up = new TextureRegionDrawable(new TextureRegion(imageUp4));
        style4.over = new TextureRegionDrawable(new TextureRegion(imageover4));
        style4.down = new TextureRegionDrawable(new TextureRegion(imageUp4));
        btn1 = new Button(style1);
        btn2 = new Button(style2);
        btn3 = new Button(style3);
        btn4 = new Button(style4);
        btn1.setPosition(Gdx.graphics.getWidth()/2-75, 400);
        btn2.setPosition(Gdx.graphics.getWidth()/2-75, 300);
        btn3.setPosition(Gdx.graphics.getWidth()/2-75, 200);
        btn4.setPosition(Gdx.graphics.getWidth()/2-75, 100);
        stage.addActor(btn1);
        stage.addActor(btn2);
        stage.addActor(btn3);
        stage.addActor(btn4);
//        String[] path=new String[15];
//        for (int i = 0; i < 15; i++) {
//            path[i]="动画/动画"+(i+1)+".png";
//        }
//        切屏动画_1 = new 切屏动画(path);
        btn1.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                next="choose";
//                切屏动画_1.start();
                if (!zhu_jie_mian.equals( null)) {
                    zhu_jie_mian.panduan();
                }
            }
        });
        btn2.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                next="game_2";
                if (!zhu_jie_mian.equals( null)) {
                    zhu_jie_mian.panduan();
                }
            }
        });
        btn3.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                next="Setting";
                if (!zhu_jie_mian.equals( null)) {
                    zhu_jie_mian.panduan();
                }
            }
        });
        btn4.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                next="Exit";
                if (!zhu_jie_mian.equals( null)) {
                    zhu_jie_mian.panduan();
                }
            }
        });
        InputMultiplexer inputMultiplexer= new InputMultiplexer();//创建一个InputMultiplexer实例
        Gdx.input.setInputProcessor(inputMultiplexer);//将inputMultiplexer设置为输入处理器
        inputMultiplexer.addProcessor(stage);



    }

    @Override
    public void render(float delta) {
        // 使用灰色清屏
        Gdx.gl.glClear(Gdx.gl.GL_COLOR_BUFFER_BIT);
        Gdx.gl.glClearColor(0.6f, 0.6f, 0.6f, 1);
        batch.begin();
        batch.draw(background, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        batch.end();
        stage.act();
        stage.draw();

    }

    @Override
    public void resize(int width, int height) {
        stage.getViewport().update(width, height, true);
    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {
        imageUp1.dispose();
        imageover1.dispose();
        imageUp2.dispose();
        imageover2.dispose();
        imageUp3.dispose();
        imageover3.dispose();
        imageUp4.dispose();
        imageover4.dispose();
        batch.dispose();
        stage.dispose();
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getNext() {
        return next;
    }
}
